package com.dcdata;

import lombok.extern.java.Log;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author: xinmi
 * @date: 2020/11/27
 * @description:
 */
@Log
public class TcpClient {

    private static String fileName;
    private static FileOutputStream fos;
    private static DataOutputStream outStream;

    private static boolean firstTime = true;

    public static void main(String[] srgc) throws IOException {

        generateFileNameSchedule();

        if (fileName == null || fileName.isEmpty()) {
            LocalDate localDate = LocalDate.now();
            fileName = System.getProperty("user.dir") + "/data/gps/GPS" + localDate.toString().replace("-", "_") + ".txt";
        }

        Socket socket = new Socket();
        SocketAddress addr = new InetSocketAddress("10.238.161.156", 18183);
        socket.connect(addr, 1000);

        fos = new FileOutputStream(fileName);
        outStream = new DataOutputStream(new BufferedOutputStream(fos));
        //socket.setSoTimeout(1000);

        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8"));
        firstTime = false;

        while (true) {
            try {
                outStream.writeUTF(reader.readLine() + "\n");
            } catch (Exception e) {
                System.out.println("写文件内容操作出错");
                e.printStackTrace();
                outStream.close();
            }

        }
    }

    private static void generateFileNameSchedule() {
        File dataDir = new File(System.getProperty("user.dir") + "/data");
        if (!dataDir.exists() && !dataDir.isDirectory())
            if (!dataDir.mkdir()) {
                System.out.println("mkdir data failed");
            }

        File gpsDir = new File(System.getProperty("user.dir") + "/data/gps");
        if (!gpsDir.exists() && !gpsDir.isDirectory())
            if (!gpsDir.mkdir()) {
                System.out.println("mkdir data/gps failed");
            }

        Timer timer = new Timer();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, Config.GPS_HOUR); //4点
        calendar.set(Calendar.MINUTE, Config.GPS_MINUTE);//45分
        calendar.set(Calendar.SECOND, Config.GPS_SECOND);//0秒
        //每天的凌晨4:45,重新生成新文件
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                //每天定时生成新的文件路径和相应的输出流
                if(firstTime == false) {
                    LocalDate localDate = LocalDate.now();
                    fileName = System.getProperty("user.dir") + "/data/gps/GPS" + localDate.toString().replace("-", "_") + ".txt";

                    try {
                        if (outStream != null){
                            outStream.close();
                        }
                        fos = new FileOutputStream(fileName);
                        outStream = new DataOutputStream(new BufferedOutputStream(fos));
                    } catch (Exception e) {
                        System.out.println("写文件内容操作出错");
                        e.printStackTrace();
                    }
                }
            }
        }, calendar.getTime(), 24 * 60 * 60 * 1000);
    }
}
